package it.pecuswap.listener;

import it.pecuswap.configuration.Configuration;
import it.pecuswap.exceptions.ConfigurationException;
import it.pecuswap.thread.BackendMonitorThread;
import it.pecuswap.thread.MergePropertiesThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

public class StartupListener implements ServletContextListener {
	
	private static final Logger LOGGER = Logger.getLogger(StartupListener.class);

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		try {
			Configuration.getInstance();
			
			//thread di monitor backend
			ExecutorService executor = Executors.newFixedThreadPool(3);
			executor.execute(BackendMonitorThread.getInstance());
			executor.execute(new MergePropertiesThread());
			
			
		} catch (ConfigurationException e) {
			LOGGER.error("Errore di configurazione. Context non inizializzato");
			throw new RuntimeException();
		}
	}

}
